﻿using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;

namespace Monti.Docking
{
   /// <summary>
   /// Coverts values for a dock site.
   /// </summary>
   internal class DockSiteBorderConverter : IValueConverter
   {
      public object Convert (object value, Type targetType, object parameter, CultureInfo culture)
      {
         if (value.GetType() == typeof(Dock))
         {
            var dockPos = (Dock)value;
            if (targetType == typeof(CornerRadius))
               return GetCornerRadius(dockPos);
         }
         else if (targetType == typeof (Thickness))
         {
            var isToolWindow = (bool) value;
            if (parameter != null)
            {
               if (parameter.ToString () == "Margin")
                  return isToolWindow ? new Thickness (0, 0, 0, 3) : new Thickness (0, 4, 0, 0);
               if (parameter.ToString () == "TabPanel")
                  return isToolWindow ? new Thickness (1, 0, 1, 1) : new Thickness (1, 1, 1, 0);
               if (parameter.ToString () == "TabBorder")
                  return isToolWindow ? new Thickness (0, 0, 1, 1) : new Thickness (0, 1, 1, 0);
            }

            return isToolWindow ? new Thickness (1, 1, 1, 0) : new Thickness (1, 0, 1, 1);
         }

         return null;
      }

      /// <summary>
      /// Gets the corner radius for the given dock position.
      /// </summary>
      /// <param name="dockPos">Dock position to test.</param>
      /// <returns>CornerRadius</returns>
      private static CornerRadius GetCornerRadius (Dock dockPos)
      {
         var radii = new CornerRadius(0.0)
            {
               TopLeft = (dockPos == Dock.Top || dockPos == Dock.Left) ? 5.0 : 0,
               TopRight = (dockPos == Dock.Top || dockPos == Dock.Right) ? 5.0 : 0,
               BottomLeft = (dockPos == Dock.Bottom || dockPos == Dock.Left) ? 5.0 : 0,
               BottomRight = (dockPos == Dock.Bottom || dockPos == Dock.Right) ? 5.0 : 0
            };
         return radii;
      }

      public object ConvertBack (object value, Type targetType, object parameter, CultureInfo culture)
      {
         throw new NotImplementedException();
      }
   }
}
